package br.com.rafael.gestorinvestimentos.DAO;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import br.com.rafael.gestorinvestimentos.model.Opcao;

public class OpcaoDAO{
	private Session session;
	
	public OpcaoDAO(Session session){
		this.session = session;
	}	
	
	public void insert (Opcao opcao){
		this.session.save(opcao);
	}
	
	public void update (Opcao opcao){
		this.session.merge(opcao);
	}
	
	public void delete (Opcao opcao){
		this.session.delete(opcao);
	}
	
	public Opcao getOpcao (int id){
		return (Opcao) session.load(Opcao.class, id);
	
	}
	
	public Opcao getOpcao (String ticker){
		Query q = this.session.createQuery("from Opcao as opcao where opcao.ticker = :opcaoTicker").setParameter("opcaoTicker", ticker);
		return (Opcao) q.uniqueResult();
	}
	
	public Opcao getOpcaoNome (String nome){
		Query q = this.session.createQuery("from Opcao as opcao where opcao.nome = :opcaoNome").setParameter("opcaoNome", nome);
		return (Opcao) q.uniqueResult();
	}
		
	public List getList (){
		return this.session.createCriteria(Opcao.class).list();
	}


}
